package models;

import Dao.DBConnect;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import Dao.DBConnect;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;

public class ShopModel  {

    Statement stmt = null;
    private int sid;
    private String shopName;
    private String shopAddr;
    private String shopTel;



    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getShopName() {
        return shopName;
    }

    @Override
    public String toString() {
        return "ShopModel{" +
                "sid=" + sid +
                ", shopName='" + shopName + '\'' +
                ", shopAddr='" + shopAddr + '\'' +
                ", shopTel='" + shopTel + '\'' +
                '}';
    }

    public void setShopName(String shopName) {
        this.shopName = shopName;
    }

    public String getShopAddr() {
        return shopAddr;
    }

    public void setShopAddr(String shopAddr) {
        this.shopAddr = shopAddr;
    }

    public String getShopTel() {
        return shopTel;
    }

    public void setShopTel(String shopTel) {
        this.shopTel = shopTel;
    }

    public void insertRecord(ShopModel sp) {

        try {
//			setCid(cid);
            // Execute a query
            System.out.println("Inserting record into the shop table...");
            stmt = DBConnect.getConnection().createStatement();
            String sql = null;

            sql = " insert into mian_wang_shops(shop_name, shop_address,shop_tel) values('" + sp.getShopName()  + "', '" + sp.getShopAddr() + "','" + sp.getShopTel() +"')";

            stmt.executeUpdate(sql);

        } catch (SQLException se) {
            se.printStackTrace();
        }
    }

    public List<ShopModel> getAll() {
        ButtonType foo = new ButtonType("OK", ButtonBar.ButtonData.OK_DONE);
        ButtonType bar = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
        Alert a = new Alert(Alert.AlertType.NONE,"",foo,bar);
        Alert b = new Alert(Alert.AlertType.CONFIRMATION,"",foo);        List<ShopModel> sps = new ArrayList<>();
        String query = "select * from mian_wang_shops  ;";
        try (PreparedStatement statement = DBConnect.getConnection().prepareStatement(query)) {
//            statement.setInt(1, cid);

            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                ShopModel sp = new ShopModel();
                // grab record data by table field name into ClientModel account object
                sp.setSid(resultSet.getInt("id"));
                sp.setShopName(resultSet.getString("shop_name"));
                sp.setShopAddr(resultSet.getString("shop_address"));
                sp.setShopTel((resultSet.getString("shop_tel")));
                sps.add(sp); // add account data to arraylist
            }

        } catch (SQLException e) {
            System.out.println("Error fetching Accounts: " + e);
        }
        System.out.println(sps.toString());
        return sps; // return arraylist
    }


}
